<template>
  <el-card class="box-card defaultTable">
    <el-table v-loading="loading" :data="d.tableData" style="width: 100%">
      <el-table-column prop="t1" label="知识" width="480">
        <template #default="scope">
          <div class="dflex">
            <div class="base-icon">
              <img :src="base_1" alt="" srcset="" />
            </div>
            <div>
              <div @click="onPush(scope.row)" class="f14666">{{ scope.row.name }}</div>
              <div class="f12999">{{ scope.row.desc }}</div>
            </div>
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="status" label="文件" />
      <el-table-column prop="size" label="尺寸" />
      <el-table-column prop="t4" label="机器人参考" />
      <el-table-column prop="create_time" label="创建时间" />
      <el-table-column align="right" label="操作">
        <template #default="scope">
          <div class="dflex justify-content-end">
            <!-- <div class="base-edit" @click="onEdit(scope.row)">
              <img :src="edit" alt="" srcset="">
            </div> -->
            <div class="base-del" @click="onDel(scope.row.id)">
              <img :src="del" alt="" srcset="" />
            </div>
          </div>
        </template>
      </el-table-column>
    </el-table>
  </el-card>

  <!-- 知识库编辑 -->
  <BaseCreated :visible="visible" type="edit" :editData="editData" @editDpData="editDpData" />
</template>

<script lang="ts" setup>
import { apIknowledDelete, knowledgeList } from "@/api/interface"
import { defaultTableType } from "@/api/interface/types/InterfaceTypes"
import base_1 from "@/assets/docs/base_1.svg"
import del from "@/assets/docs/del.svg"
import BaseCreated from "@/components/Create/components/baseCreated.vue"
import { ElMessage, ElMessageBox } from "element-plus"
import pubsub from "pubsub-js"
import { onBeforeUnmount, onMounted, provide, reactive, ref } from "vue"
import { useRouter } from "vue-router"

const router = useRouter()
const visible = ref<boolean>(false)
const editData = ref({})
const loading = ref<boolean>(false)
provide("visible", visible)

// 初始化知识库
const initModel = async () => {
  loading.value = true
  let params = {
    page: 1
  }
  let { data } = await knowledgeList(params)

  d.tableData = data.list
  loading.value = false
}
initModel()

let KNOWLEDCREATE = null as any

onMounted(() => {
  KNOWLEDCREATE = pubsub.subscribe("KNOWLEDCREATE", () => {
    // 目前后端同学没给额外参数 说创建成功重新拉列表- -
    // 目前数据不多无影响 如果存在分页情况 需要后端额外的参数来补齐列表回显
    initModel()
  })
})

onBeforeUnmount(() => {
  pubsub.unsubscribe(KNOWLEDCREATE)
})

// 编辑回显
const editDpData = (p: any) => {
  console.log(p)
  d.tableData = d.tableData.map((item) => {
    if (item.id === p.id) {
      item = p
    }
    return item
  })
  // 关闭编辑
  visible.value = false
}

//直接跳转
const onPush = (params: any) => {
  //带参数跳转
  // router.push({ path: '/knowledgeBase/components/knowledgeDetails', query: {id} });
  router.push({
    name: "KnowledgeDetails",
    state: {
      params: JSON.stringify(params)
    }
  })
}

// mock
const d = reactive<defaultTableType>({
  tableData: []
})
const delFn = (p: number) => {
  let newList = d.tableData.filter((e) => e.id !== p)
  d.tableData = newList
}

// 编辑
// const onEdit = (e: any) => {
//   // 深拷贝
//   const newData = _.cloneDeep(e)
//   // 编辑传入参数
//   editData.value = newData
//   // 打开编辑
//   visible.value = true
// }
// 删除
const onDel = (id: number) => {
  ElMessageBox.confirm("相关记忆体将永久删除，请谨慎操作！是否继续删除？", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning"
  })
    .then(async () => {
      let { code } = await apIknowledDelete({ id })
      if (code === 200) {
        console.log(code)
        delFn(id)
        ElMessage({
          type: "success",
          message: "删除成功"
        })
      }
    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "删除失败"
      })
    })
}
</script>
<style lang="scss" scoped>
.defaultTable .base-icon {
  width: 40px;
  height: 40px;
  flex-shrink: 0;
  border-radius: 10px;
  background: var(--v3-sidebar-menu-hover-bg-color);
  text-align: center;
  line-height: 55px;
  margin-right: 10px;
}

.el-card {
  box-shadow: none;
  border: none;
}

.base-edit {
  margin-right: 20px;
  cursor: pointer;
}

.f14666 {
  cursor: pointer;
}

.f14666 :hover {
  color: #c3181f;
}
</style>
